﻿<%@ WebHandler Language="C#" Class="ASH001" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using Com.LongER.Business;
using Com.LongER.Business.COMMON;

public class ASH001 : IHttpHandler {
    
    /// <summary>
    /// 取客户
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest (HttpContext context) {

        String strKEHU_CD = context.Request.QueryString["KEHU_CD"];
        if (strKEHU_CD == null)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("xxx");
            return;
        }
        strKEHU_CD = strKEHU_CD.ToUpper();

        String strGONGSI_XINGZHI = context.Request.QueryString["GONGSI_XINGZHI"];
        
        CCOMMON instance = new CCOMMON();

        COMMONDS.M_KEHUDataTable dt = instance.Get客户();

        var query = from data in dt
                    where data.KEHU_CD.StartsWith(strKEHU_CD)
                    select new
                    {
                        KEHU_CD = data.KEHU_CD,
                        KEHU_HANZI = data.KEHU_HANZI
                    };
        
        //
        if (strGONGSI_XINGZHI != null && strGONGSI_XINGZHI != String.Empty)
        {
           
            query = from data in dt
                    where data.KEHU_CD.StartsWith(strKEHU_CD)
                        && data.GONGSI_XINGZHI == strGONGSI_XINGZHI
                    select new
                    {
                        KEHU_CD = data.KEHU_CD,
                        KEHU_HANZI = data.KEHU_HANZI
                    };
        }
        
        Boolean isExistQuery = false;

        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("[");
        
        foreach (var r in query)
        {
            isExistQuery = true;
            jsonBuilder.Append("{");
            jsonBuilder.Append("\"KEHU_CD\":").Append("\"").Append(r.KEHU_CD).Append("\"").Append(",");
            jsonBuilder.Append("\"KEHU_HANZI\":").Append("\"").Append(r.KEHU_HANZI).Append("\"");
            jsonBuilder.Append("},");
        }
        
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");

        //context.Response.Clear();
        context.Response.Charset = "utf-8";
        context.Response.Buffer = true;
        context.Response.ContentEncoding = System.Text.Encoding.UTF8; 
        context.Response.ContentType = "text/plain";
        context.Response.Write(isExistQuery==true? jsonBuilder.ToString():"");
        //context.Response.Flush();
        //context.Response.Close();
        //context.Response.End();
        
    }
 
    /// <summary>
    /// 
    /// </summary>
    public bool IsReusable {
        get {
            return false;
        }
    }

    /// <summary>
    /// dummmy [{label: "博客园", value: "cnblogs"}, {label: "囧月", value: "囧月"}]
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public string DataTable2Json(DataTable dt)
    {
        if (dt.Rows.Count == 0)
        {
            return "";
        }

        StringBuilder jsonBuilder = new StringBuilder();
        // jsonBuilder.Append("{"); 
        //jsonBuilder.Append(dt.TableName.ToString());  
        jsonBuilder.Append("[");//转换成多个model的形式
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        //  jsonBuilder.Append("}");
        return jsonBuilder.ToString();
    }
    
}